import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:岁亮亮
 * Date:2024-01-10
 * TIME:16:14
 */
public class MyArrayList implements List,Cloneable {
    public int[] elem;
    private static final int MAX=4;
    public int usednum=0;


    public MyArrayList() {//默认的数组;
        this.elem = new int[MAX];
        elem[0]=13;
        elem[1]=34;
        elem[2]=23;
        usednum=3;
    }

    public MyArrayList(int fulm){
        this.elem=new int[fulm];
    }

    public int getUsednum() {
        return usednum;
    }

    public void setUsednum(int usednum) {
        this.usednum = usednum;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean isfull() {
        return usednum==elem.length;
    }

    @Override
    //默认在数组最后新增
    public void add(int data) {
        //先判断是否满了需不需要扩容
        checkisfull();
        this.elem[this.usednum]=data;
        usednum++;
    }
    private void checkisfull(){
        if(isfull()){
            //扩容
            elem= Arrays.copyOf(elem,elem.length*2);
        }
    }
    private void checkAddPos(int pos)throws posIllegality{
        if(pos<1||pos>usednum){
            System.out.println("在增加的位置是不合法的    合法区间是[1,"+usednum+"]"+"你当前要增加的位置在 " +pos);
            throw new posIllegality("插入元素下标越界异常");
        }
    }
    @Override
    //在pos位置加入data
    public void add(int pos, int data) {
        checkisfull();
        //在数组结构当中每次存储数据的时候   必须有一个前驱信息 所以不能插入到前面没有数据的后面
       try {
           checkAddPos(pos);
       }catch (posIllegality p){
           p.printStackTrace();
           //尽可能在要发异常时完成用户尽可能对的操作
           if(pos<0){
               pos=-pos;
               for (int i = usednum; i >=pos ; i--) {
                   this.elem[i]=this.elem[i-1];
               }
               this.elem[pos-1]=data;
               usednum++;
           }
           else{
               pos=usednum;
               add(data);
           }

           return;
       }
        for (int i = usednum; i >=pos ; i--) {
            this.elem[i]=this.elem[i-1];
        }
        this.elem[pos-1]=data;
        usednum++;

    }

    @Override
    public boolean isEmpty() {
        if(this.elem.length==0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty() ){
            return false;
        }
        for (int i = 0; i < usednum; i++) {
            if(this.elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty() ){
            return -1;
        }
        for (int i = 0; i < usednum; i++) {
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        checkPosGetandSet(pos) ;
        if(isEmpty()){
            throw new MyArrrayListEmpty("数组为空异常");
        }
        return elem[pos];
    }
     private void checkPosGetandSet(int pos)throws posIllegality {
        if(pos<0||pos>=this.usednum){
            System.out.println("查找下标越界异常"+pos);
            throw new posIllegality("查找下标越界异常") ;
        }
     }
    @Override
    public void set(int pos, int value) {
        checkPosGetandSet(pos);
        this.elem[pos]=value;

    }

    @Override
    public void remove(int toRemove) {
        int index=indexOf(toRemove);
        if(index==-1){
            System.out.println("没有找到");
        }
        else{
            for (int i = index; i <usednum-1 ; i++) {
                this.elem[i]=this.elem[i+1];
            }
        }
        usednum--;

    }

    @Override
    public int size() {
        return this.usednum;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i < this.usednum; i++) {
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();

    }
}
